// pages/articleDetail/articleDetail.js

var WxParse = require('../../wxParse/wxParse.js');
var that;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    item:'',
    id:'',
    article:'',
    is_collection:false,
    is_praise:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    that.setData({
      id: options.id
    })
    wx.showToast({
      title: '加载中',
      icon: 'loading'
    })
    setTimeout(() => {
      that.dataLoadingIndex()
      that.checkZanArticle()
      that.checkCollectArticle()
    }, 1000)
    
  },
  //通过文章id查询文章
  dataLoadingIndex: function () {
    wx.request({
      url: getApp().globalData.urls +'/wxgz/article/getArticleById?articleId=' + that.data.id,
      success: function (res) {
        //console.log(res.data)
        var code = res.data.code
        if (code == 200) {
          var data = res.data.data;
          WxParse.wxParse('article', 'html', data.content, that, 30)
          that.setData({
            item: data,
          })
        } else {

        }

      }
    })
  },

//获取用户是否点赞
  checkZanArticle:function(){
    
    wx.request({
      url: getApp().globalData.urls + '/wxgz/articleManage/checkZanArticle',
      method: "POST",
      data: {
        articleId: that.data.id,
        token: wx.getStorageSync("token")
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // post:application/x-www-form-urlencoded,get:application/json
      },
      success:function(res){
        console.log(res.data)
        if(res.data.code == 200){
          that.setData({
            is_praise:res.data.data
          })
        }
      }

    })
  },
  //点赞与取消点赞
  saveZanArticle:function(){
    wx.showLoading({
      title: '',
    })
    wx.request({
      url: getApp().globalData.urls + '/wxgz/articleManage/saveZanArticle',
      method: "POST",
      data: {
        articleId: that.data.id,
        token: wx.getStorageSync("token"),
        state: that.data.is_praise? 1:2,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // post:application/x-www-form-urlencoded,get:application/json
      },
      success:function(res){
        wx.hideLoading()
        console.log(res.data)
        
        if (res.data.code == 200) {
          that.setData({
            is_praise: !that.data.is_praise
          })
          wx.showToast({
            title: res.data.msg,
          })
        }else{
          wx.showToast({
            title: res.data.msg,
          })
        }
      },
      fail:function(res){
        wx.hideLoading()
      }
    })
  },

    //获取用户是否收藏过文章
  checkCollectArticle:function(){
  
    wx.request({
      url: getApp().globalData.urls + '/wxgz/articleManage/checkCollectArticle',
      method: "POST",
      data: {
        articleId: that.data.id,
        token: wx.getStorageSync("token")
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // post:application/x-www-form-urlencoded,get:application/json
      },
      success: function (res) {
       
        console.log(res.data)
        if (res.data.code == 200) {
          that.setData({
            is_collection: res.data.data
          })
        }
       
      }

    })
  },
  collectArticle:function(){
    wx.showLoading({
      title: '',
    })
    wx.request({
      url: getApp().globalData.urls + '/wxgz/articleManage/collectArticle',
      method: "POST",
      data: {
        articleId: that.data.id,
        token: wx.getStorageSync("token"),
        state: that.data.is_collection ? 1 : 2,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // post:application/x-www-form-urlencoded,get:application/json
      },
      success: function (res) {
        wx.hideLoading()
        console.log(res.data)

        if (res.data.code == 200) {
          that.setData({
            is_collection: !that.data.is_collection
          })
        }
        wx.showToast({
          title: res.data.msg,
        })
      },
      fail: function () {
        wx.hideLoading()
      }
    })
  },
  



  



  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (ops) {

    return{
      title:that.data.item.title,
      title: that.data.item.title,
      path: '/pages/articleDetail/articleDetail?id=' + that.data.id,
      imageUrl: that.data.item.upimageUrl,
      success:function(res){
        if (ops.from === 'button') {
          // 来自页面内转发按钮
          wx.request({
            url: getApp().globalData.urls + '/wxgz/articleManage/sendArticle',
            data: {
              articleId: that.data.id
            },
            method: "POST",
            header: {
              'content-type': 'application/x-www-form-urlencoded' // post:application/x-www-form-urlencoded,get:application/json
            },
            success: function (res) {
              console.log(res.data)

              if (res.data.code == 200) {
                wx.showToast({
                  title: '转发成功',
                })
              }
            },
            fail: function (res) {

            }
          })
        }
      },
      fail:function(res){
      
      }
    }
  }
})